<?php

namespace  App\Http\Controllers;

//兑换物品
use App\Common\Sql;

class ExchangeItem extends Base
{

    public function logic()
    {
        if (!isset($this->param['uid'])) {
            return $this->returnError(1, "please input uid");
        }
        if (!isset($this->param['item_id'])) {
            return $this->returnError(2, "please input item_id");
        }
        if (!isset($this->param['action_id'])) {
            return $this->returnError(3, "please input action_id");
        }
        $uid       = $this->param['uid'];
        $cur_time  = time();
        $item_id   = $this->param['item_id'];
        $action_id = $this->param['action_id'];

        $data      = array();
        $row = Sql::select('t_item_exchange_conf',[['status',1],['id',$action_id]]);
        if (count($row) != 1) {
            return $this->returnError(4, "action_id isn't exists");
        } else {
            $ret        = false;
            $act_info   = array();
            $info       = $row[0];
            $begin_time = strtotime($info['begin_time']);
            $end_time   = strtotime($info['end_time']);
            //检查是否在活动时间内
            if ($cur_time < $begin_time || $cur_time > $end_time) {
                return $this->returnError(5, "The action has expired");
            }
            $actid    = (int)$info['id'];
            $currency = (int)$info['currency'];
            $rule     = $info['rule'];
            //解析兑换比例
            $rules = explode('|', $rule);
            foreach ($rules as $items) {
                $item_num = explode('=', $items);
                if (count($item_num) != 2) {
                    continue;
                }
                $item = (int)$item_num[0];
                $num  = (int)$item_num[1];
                if ($item == $item_id) {
                    if ($this->sub_user_item($uid, $currency, $num)) {
                        $ret = $this->reward_item($uid, $item_id, 1, "兑换活动", "action:" . (string)$actid);
                        if (!$ret) {
                            if (!$this->add_user_item($uid, $currency, 1, "兑换物品失败回滚")) {
                                return $this->returnError(6, "The item rollback fail! please tell our customer service");
                            }
                        }
                    }
                    break;
                }
            }
            if ($ret) {
                //获取用户拥有该物品的数量
                $uid_has_item_num       = $this->get_user_item_num($uid, $currency);
                $act_info['currency']   = array('id' => $currency, 'user_has' => $uid_has_item_num);
                $act_info['id']         = $actid;
                $act_info['name']       = $info['name'];
                $act_info['begin_time'] = $info['begin_time'];
                $act_info['end_time']   = $info['end_time'];
                $data['action_info']    = $act_info;
            } else {
                return $this->returnError(7, "The item exchange fail!");
            }
        }
        $data['uid'] = $uid;
        return $this->returnData($data);
    }
}